/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.rogrand.modules.hp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.rogrand.common.config.Global;
import com.thinkgem.rogrand.common.persistence.Page;
import com.thinkgem.rogrand.common.web.BaseController;
import com.thinkgem.rogrand.common.utils.StringUtils;
import com.thinkgem.rogrand.modules.hp.entity.HpOwner;
import com.thinkgem.rogrand.modules.hp.service.HpOwnerService;

/**
 * 业主Controller
 * @author mountain
 * @version 2017-05-09
 */
@Controller
@RequestMapping(value = "${adminPath}/hp/hpOwner")
public class HpOwnerController extends BaseController {

	@Autowired
	private HpOwnerService hpOwnerService;
	
	@ModelAttribute
	public HpOwner get(@RequestParam(required=false) String id) {
		HpOwner entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hpOwnerService.get(id);
		}
		if (entity == null){
			entity = new HpOwner();
		}
		return entity;
	}
	
	@RequiresPermissions("hp:hpOwner:view")
	@RequestMapping(value = {"list", ""})
	public String list(HpOwner hpOwner, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HpOwner> page = hpOwnerService.findPage(new Page<HpOwner>(request, response), hpOwner); 
		model.addAttribute("page", page);
		return "modules/hp/hpOwnerList";
	}

	@RequiresPermissions("hp:hpOwner:view")
	@RequestMapping(value = "form")
	public String form(HpOwner hpOwner, Model model) {
		model.addAttribute("hpOwner", hpOwner);
		return "modules/hp/hpOwnerForm";
	}

	@RequiresPermissions("hp:hpOwner:edit")
	@RequestMapping(value = "save")
	public String save(HpOwner hpOwner, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hpOwner)){
			return form(hpOwner, model);
		}
		hpOwnerService.save(hpOwner);
		addMessage(redirectAttributes, "保存业主成功");
		return "redirect:"+Global.getAdminPath()+"/hp/hpOwner/?repage";
	}
	
	@RequiresPermissions("hp:hpOwner:edit")
	@RequestMapping(value = "delete")
	public String delete(HpOwner hpOwner, RedirectAttributes redirectAttributes) {
		hpOwnerService.delete(hpOwner);
		addMessage(redirectAttributes, "删除业主成功");
		return "redirect:"+Global.getAdminPath()+"/hp/hpOwner/?repage";
	}

}